/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.ccn.sac.net;

import uk.ac.cam.ccn.sac.modules.privatemessage.PrivateTextListener;
import uk.ac.cam.ccn.sac.modules.publicmessage.TextListener;
import uk.ac.cam.ccn.sac.modules.audio.message.AudioListener;
import uk.ac.cam.ccn.sac.modules.audio.message.AudioMessage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.io.content.ContentEncodingException;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.MalformedContentNameStringException;
import uk.ac.cam.ccn.sac.modules.contacts.Contact;

/**
 *
 * @author Thomas
 */
public class CCNNet {    
    public interface TextCallBack{
        public void textReceived(String message);
    }
    
    public interface AudioCallBack{
        public void audioReceived(AudioMessage message);
    }
    
    public interface ContactCallBack{
        public void contactReceived(Contact contact);
    }
    private static final CCNNet instance = new CCNNet();
    
    private final List<AudioCallBack> audioCallBacks = new ArrayList<AudioCallBack>();
    private final List<TextCallBack> textCallBacks = new ArrayList<TextCallBack>();
    private final List<ContactCallBack> contactsCallBacks = new ArrayList<ContactCallBack>();
    
    private TextListener tl;
    private AudioListener al;
    private PrivateTextListener ptl;
    private ContentName nameSpace;
    
    private Contact contact;
    
    public static CCNNet getInstance(){
        return instance;
    }
    
    private CCNNet(){
    }
    
    public void setLocalContact(Contact contact){
        this.contact=contact;
    }
    
    public Contact getLocalContact(){
        return contact;
    }
    
    public void showMessage(String message){
        for(TextCallBack callback : textCallBacks)
            callback.textReceived(message);
    }
    
    public void playAudio(AudioMessage message){
        for(AudioCallBack callback : audioCallBacks)
            callback.audioReceived(message);
    }
    
    public void newContact(Contact contact){
        for(ContactCallBack callback : contactsCallBacks)
            callback.contactReceived(contact);
    }
    
    public void sendAudio(AudioMessage audio) throws ContentEncodingException, IOException {
        al.sendAudio(audio);
    }
    
    public void sendText(String msg) throws ContentEncodingException, IOException{
        tl.sendText(msg);
    }

    public void sendPrivateText(String to, String msg) throws ContentEncodingException, IOException, MalformedContentNameStringException, ConfigurationException{
        ptl.sendText(to, msg);
    }
    
    public void shutdown(){
        tl.shutdown();
        al.shutdown();
        ptl.shutdown();
    }
    
    public void initialize(String ns) throws MalformedContentNameStringException, ConfigurationException, IOException{
        nameSpace=ContentName.fromURI(ns);
        if (nameSpace.toString().startsWith("ccnx:/")) {
                UserConfiguration.setDefaultNamespacePrefix(nameSpace.toString().substring(5));		
        } else {
                UserConfiguration.setDefaultNamespacePrefix(nameSpace.toString());
        }
        
        tl = new TextListener(this, nameSpace);
        al = new AudioListener(this, nameSpace);
        ptl = new PrivateTextListener(this);
    }

    public void addContactsCallBack(ContactCallBack callback){
        contactsCallBacks.add(callback);
    }

    public void addTextCallBack(TextCallBack callback){
        textCallBacks.add(callback);
    }

    public void addAudioCallBack(AudioCallBack callback){
        audioCallBacks.add(callback);
    }
    
    public void start(){
        tl.start();
        al.start();
        ptl.start();
    }
}
